package com.thb.pay.manage.web.sys;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.thb.pay.api.sys.vo.req.SysUserPageReq;
import com.thb.pay.api.sys.vo.req.SysUserReq;
import com.thb.pay.api.sys.vo.req.SysUserRoleReq;
import com.thb.pay.api.sys.vo.resp.SysRoleVo;
import com.thb.pay.api.sys.vo.resp.SysUserRoleVo;
import com.thb.pay.api.sys.vo.resp.SysUserVo;
import com.thb.pay.core.annotation.OperationLog;
import com.thb.pay.core.enums.ExceptionEnum;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.core.utils.R;
import com.thb.pay.manage.comm.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/sys/user")
public class SysUserController extends BaseController {
    /**
     * @param id id
     * @return
     * @方法名称
     * @功能描述 查询
     * @作者: lucky
     * @创建时间: 16:31 2019/10/11
     * @版本 v1.01
     **/
    @RequestMapping("/get")
    public R get(@RequestParam("id") Long id) {
        SysUserVo resultData = feginCommService.sysUserServiceApi.getByUserId(id);
        return R.okData(resultData);
    }

    /**
     * 新增用户信息
     *
     * @return
     */
    @PostMapping("/add")
    @OperationLog(value = "新增用户")
    public R add(@RequestBody SysUserReq sysUserReq) {
        SysUserVo sysUserVo = feginCommService.sysUserServiceApi.getByUserName(sysUserReq.getUserName());
        if (sysUserVo != null) {
            throw RRException.build(ExceptionEnum.USERNAME_EXIST);
        }
        feginCommService.sysUserServiceApi.addUser(sysUserReq);
        return R.ok();
    }

    /**
     * 修改用户信息
     *
     * @return
     */
    @PostMapping("/update")
    @OperationLog(value = "修改用户")
    public R update(@RequestBody SysUserReq sysUserReq) {
        if (StringUtils.isNotEmpty(sysUserReq.getPassword())) {
            sysUserReq.setLastPasswordResetTime(new Date());
        }
        SysUserVo sysUserVo = feginCommService.sysUserServiceApi.getByUserName(sysUserReq.getUserName());
        if (sysUserVo != null && sysUserVo.getId().compareTo(sysUserReq.getId()) != 0) {
            throw RRException.build(ExceptionEnum.USERNAME_EXIST);
        }
        feginCommService.sysUserServiceApi.updateUser(sysUserReq);
        return R.ok();
    }

    @PostMapping("/list")
    public R list(@RequestBody SysUserPageReq pageReq) {
        PageResult<SysUserVo> pageResult = feginCommService.sysUserServiceApi.findPage(pageReq);
        return R.okData(pageResult);
    }

    /**
     * 删除用户信息
     *
     * @return
     */
    @PostMapping("/delete")
    @OperationLog(value = "删除用户")
    public R delete(@RequestParam("userIds") String userIds) {
        List<Long> targetUserIds = parseStrIdsToList(userIds);
        feginCommService.sysUserServiceApi.deleteBatchUserIds(targetUserIds);
        return R.ok();
    }

    /**
     * 查看用户所有角色
     *
     * @param userId
     * @return
     */
    @GetMapping("/listRoleByUserId")
    public R listRoleByUserId(@RequestParam("userId") Long userId) {
        // 该用户分配的角色
        List<SysUserRoleVo> sysUserRoleList = feginCommService.sysUserServiceApi.findUserRolesByUserId(userId);
        // 得到所有角色
        List<SysRoleVo> sysRoleList = feginCommService.sysRoleServiceApi.findAllRoles();
        JSONArray resultList = new JSONArray();
        for (SysRoleVo sysRole : sysRoleList) {
            JSONObject object = new JSONObject();
            object.put("title", sysRole.getName());
            object.put("value", sysRole.getId());
            object.put("disabled", false);
            object.put("data", new ArrayList<>());
            object.put("checked", false);
            // 设置是否被选中
            for (SysUserRoleVo sysUserRole : sysUserRoleList) {
                if (sysRole.getId().compareTo(sysUserRole.getRoleId()) == 0) {
                    object.put("checked", true);
                    break;
                }
            }
            resultList.add(object);
        }
        return R.okData(resultList);
    }

    /**
     * 保存用户的角色
     *
     * @param userRoleReq
     * @return
     */
    @PostMapping("/saveUserRole")
    @OperationLog(value = "修改用户角色")
    public R saveUserRole(@RequestBody SysUserRoleReq userRoleReq) {
        feginCommService.sysUserServiceApi.addUserRoles(userRoleReq);
        return R.ok();
    }

}